<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户id" prop="uid">
        <el-input
            v-model="queryParams.uid"
            placeholder="请输入用户id"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入用户名"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery" v-has-permission="['ums:integration:list']">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        <el-button
            type="danger"
            icon="el-icon-delete"
            size="small"
            :disabled="multiple"
            @click="handleDelete"
            v-has-permission="['ums:integration:delete']"
        >删除
        </el-button>
        <el-button
            type="info"
            icon="el-icon-download"
            size="small"
            @click="handleExport"
            v-has-permission="['ums:integration:export']"
        >导出
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="integration_change_historyList" @selection-change="handleSelectionChange" border :header-cell-style="{background:'#EAE9E9FF'}">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="#" align="center" prop="id"/>
      <!--      <el-table-column label="用户id" align="center" prop="uid"/>-->
      <!--      <el-table-column label="充值id" align="center" prop="rechargeId"/>-->
      <el-table-column label="昵称" align="center" prop="nickname"/>
      <el-table-column label="真实姓名" align="center" prop="realName"/>
      <el-table-column label="积分" align="center" prop="integration">
        <template slot-scope="scope">
          <div v-if="scope.row.integration < 0" style="color: red">{{ scope.row.integration }}</div>
          <div v-if="scope.row.integration > 0" style="color: green">{{ scope.row.integration }}</div>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center" prop="createTime"/>
      <el-table-column label="备注" width="300px">
        <template slot-scope="scope">
          <span>{{ scope.row.remark }}</span>
          <span v-if="scope.row.rechargeId">...</span>
          <el-button v-if="scope.row.rechargeId" type="text" @click="rechargeDetailHandler(scope.row.rechargeId)" v-has-permission="['oms:order:infoByOrderSn']">查看详情</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              v-has-permission="['ums:integration:delete']"
              size="small"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="pagination.total > 0"
        :total="pagination.total"
        :page.sync="pagination.pageNum"
        :limit.sync="pagination.pageSize"
        @pagination="handleQuery"
    />
    <!-- 添加或修改积分变化历史记录对话框 -->
    <el-dialog :title="dialog.title" :visible.sync="dialog.visible" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="member_id" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入member_id"/>
        </el-form-item>
        <el-form-item label="充值id" prop="rechargeId">
          <el-input v-model="form.rechargeId" placeholder="请输入充值id"/>
        </el-form-item>
        <el-form-item label="变化的值" prop="integration">
          <el-input v-model="form.integration" placeholder="请输入变化的值"/>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入昵称"/>
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入真实姓名"/>
        </el-form-item>
        <el-form-item label="用户username" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户username"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--积分来源详情对话框 -->
    <el-dialog :title="dialog.title" :visible.sync="dialog.rechargeDetailVisible" width="1000px" append-to-body>
      <el-table
          v-loading="loading"
          :data="rechargeList"
          size="small"
          border
          style="width: 100%;text-align-all: center"
          :header-cell-style="{background:'#EAE9E9FF'}"
          @selection-change="handleSelectionChange"
      >
        <el-table-column prop="nickname" label="充值用户"/>
        <el-table-column prop="phone" label="手机号"/>
        <el-table-column prop="levelId" label="类型">
          <template slot-scope="scope">
            <div v-if="scope.row.levelId === undefined">余额充值</div>
            <div v-if="scope.row.levelId !== undefined">升级充值</div>
          </template>
        </el-table-column>
        <el-table-column prop="applyTime" label="申请充值时间"/>
        <!--      <el-table-column prop="rechargeType" label="充值类型">-->
        <!--        <template slot-scope="scope">-->
        <!--          <div v-if="scope.row.rechargeType=== 0">正常充值</div>-->
        <!--        </template>-->
        <!--      </el-table-column>-->
        <el-table-column prop="otPrice" label="充值金额" width="90%"/>
        <el-table-column prop="price" label="实际到账金额" width="90%"/>
        <el-table-column prop="rechargeMode" label="充值方式">
          <template slot-scope="scope">
            <div v-if="scope.row.rechargeMode=== 0">微信</div>
            <div v-if="scope.row.rechargeMode=== 2" style="color: green">线下打款</div>
            <div v-if="scope.row.rechargeMode=== 1">支付宝</div>
            <div v-if="scope.row.rechargeMode=== 3">上级充值</div>
          </template>
        </el-table-column>
        <el-table-column prop="prove" label="充值证明">
          <template slot-scope="scope">
            <el-image :src="scope.row.prove" style="width: 50px;height: 50px" :preview-src-list="[scope.row.prove]">
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline"/>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="审核备注"/>
        <el-table-column prop="examineTime" label="审核时间"/>
        <el-table-column prop="status" label="状态" width="80px">
          <template slot-scope="scope">
            <div v-if="scope.row.status === 1" style="color: #13ce66;font-weight: bold">
              已处理
            </div>
            <div v-if="scope.row.status === -1" style="color: black;">
              已驳回
            </div>
            <div v-if="scope.row.status === 0" style="color: red;font-weight: bold">
              待审核
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { downloadFile } from '@/utils'
import {
  getIntegration_change_history,
  addIntegration_change_history,
  delIntegration_change_history,
  exportIntegration_change_history,
  listIntegration_change_history,
  updateIntegration_change_history
} from '@/api/ums/integration'
import { info } from '@/api/ums/userRecharge'
import checkPermission from '@/utils/permission'

export default {
  // name: 'Integral',
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      pagination: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      // 积分变化历史记录表格数据
      integration_change_historyList: [],
      // 弹出层
      dialog: {
        title: undefined,
        visible: false,
        rechargeDetailVisible: false,
        type: undefined // type 操作类型：1-新增 2-修改
      },
      rechargeList: [],
      // 查询参数
      queryParams: {
        uid: null,
        rechargeId: null,
        integration: null,
        nickname: null,
        realName: null,
        username: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    checkPermission,
    /** 查询积分变化历史记录列表 */
    getList() {
      this.loading = true
      Object.keys(this.queryParams).forEach(key => {
        this.queryParams[key] == '' ? this.queryParams[key] = null : false
      })
      this.queryParams.pageNum = this.pagination.pageNum
      this.queryParams.pageSize = this.pagination.pageSize
      listIntegration_change_history(this.queryParams).then(response => {
        const { data } = response
        this.integration_change_historyList = data.list
        this.pagination.total = data.totalCount
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.reset()
      this.closeDialog()
    },
    handleReset() {
      this.pagination = {
        pageNum: 1,
        pageSize: 10,
        total: 0
      }
      this.queryParams = {
        uid: null,
        rechargeId: null,
        integration: null,
        nickname: null,
        realName: null,
        username: null
      }
      this.handleQuery()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        uid: null,
        rechargeId: null,
        integration: null,
        nickname: null,
        realName: null,
        username: null,
        remark: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        isDelete: null
      }
      this.resetForm('form')
      this.rechargeList = []
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    resetForm(val) {
      if (this.$refs[val]) {
        this.$refs[val].resetFields()
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.dialog = {
        title: '添加积分变化历史记录',
        visible: true,
        type: 'add'
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.dialog = {
        title: '修改积分变化历史记录',
        visible: true,
        type: 'edit'
      }
      const id = row.id || this.ids
      getIntegration_change_history(id).then(response => {
        this.form = response.data
        // this.open = true;
        // this.title = "修改积分变化历史记录";
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.dialog.type === 'edit') {
            updateIntegration_change_history(this.form).then(response => {
              this.$message.success('修改成功')
              this.closeDialog()
              this.getList()
            })
          } else {
            addIntegration_change_history(this.form).then(response => {
              this.$message.success('新增成功')
              this.closeDialog()
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id ? [row.id] : this.ids
      this.$confirm('是否确认删除积分变化历史记录编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delIntegration_change_history(ids)
      }).then(() => {
        this.getList()
        this.$message.success('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      delete queryParams.pageNum
      delete queryParams.pageSize
      this.$confirm('是否确认导出所有积分变化历史记录数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportIntegration_change_history(queryParams)
      }).then(response => {
        downloadFile(response.data, '积分记录数据', 'xls')
      })
    },
    rechargeDetailHandler(id) {
      this.dialog = {
        title: '积分详情',
        rechargeDetailVisible: true
      }
      info(id).then(res => {
        this.rechargeList = [res.data]
      })
    },
    closeDialog() {
      this.dialog = {
        title: undefined,
        visible: false,
        rechargeDetailVisible: false,
        type: undefined
      }
    }
  }
}
</script>
